# -*- coding: utf-8 -*-
import KBEngine
import IKernel
import random
import MsgDef
import RoomDef
from KBEDebug import *

class HallModule:
	def __init__(self):
		self.curRoomID = random.randint(2000,3000)
		self.hallEntity = None

	def init(self, isReload):
		# 注册回调函数
		IKernel.Instance().registerFunc("HallModule.onHallGetCell", self.onHallGetCell)
		IKernel.Instance().registerFunc("HallModule.onAccountGetCell", self.onAccountGetCell)
		IKernel.Instance().registerFunc("HallModule.onRoomGetCell", self.onRoomGetCell)
		IKernel.Instance().registerFunc("HallModule.onRoomLoseCell", self.onRoomLoseCell)
		IKernel.Instance().registerFunc("HallModule.onCellMsg", self.onCellMsg)
		IKernel.Instance().registerFunc("HallModule.onAccountClientEnabled", self.onAccountClientEnabled)

		# 添加RoomEntity的onGetCell事件
		IKernel.Instance().addEventCallback("Room", "onGetCell", "HallModule.onRoomGetCell")
		IKernel.Instance().addEventCallback("Room", "onLoseCell", "HallModule.onRoomLoseCell")

		IKernel.Instance().addEventCallback("Account", "onGetCell", "HallModule.onAccountGetCell")
		IKernel.Instance().addEventCallback("Hall", "onGetCell", "HallModule.onHallGetCell")
		IKernel.Instance().addEventCallback("Account", "onClientEnabled", "HallModule.onAccountClientEnabled")
		IKernel.Instance().addIntMsgCallback(MsgDef.CELL2BASE_HALL_MSG, "HallModule.onCellMsg")

		self.hallEntity = KBEngine.createEntityLocally("Hall")
		self.hallEntity.createCellEntityInNewSpace(1)

	def end(self):
		pass

	def onCellMsg(self, senderEntityID, receiverEntityID, args):
		dispatcher = {
			RoomDef.SUBMSG_CREATE_EMPTY_ROOM : self.createEmptyRoom,
		}

		submsg = args[1]
		funcAddr = dispatcher.get(submsg)
		if funcAddr == None:
			return
		funcAddr(senderEntityID, receiverEntityID, args)

	def createEmptyRoom(self, senderEntityID, receiverEntityID, args):
		"""
		创建空房间
		"""
		for i in range(RoomDef.OnceCreateEmptyRoomNum):
			self.curRoomID += 1

			roomEntity = KBEngine.createEntityLocally("Room")
			roomEntity.cellData["roomID"] = self.curRoomID
			roomEntity.createCellEntityInNewSpace(1)

	def onRoomGetCell(self, eventType, eventName, senderEntityID, receiverEntityID, args):
		argsList = []
		argsList.append(MsgDef.BASE2CELL_HALL_MSG)
		argsList.append(RoomDef.SUBMSG_CREATE_EMPTY_ROOM)
		IKernel.Instance().sendCellMsg(senderEntityID, receiverEntityID, argsList)

	def onRoomLoseCell(self, eventType, eventName, senderEntityID, receiverEntityID, args):
		roomEntityID = receiverEntityID
		roomEntity = KBEngine.entities.get(roomEntityID)
		if roomEntity == None:
			# 正常的话，这里不会触发
			assert(False)
		roomEntity.destroy()

	def onHallGetCell(self, eventType, eventName, senderEntityID, receiverEntityID, args):
		argsList = []
		argsList.append(MsgDef.BASE2CELL_HALL_MSG)
		argsList.append(RoomDef.SUBMSG_CREATE_HALL_SUCCESS)
		IKernel.Instance().sendCellMsg(senderEntityID, receiverEntityID, argsList)

	def onAccountClientEnabled(self, eventType, eventName, senderEntityID, receiverEntityID, args):
		accountEntityID = receiverEntityID
		accountEntity = KBEngine.entities.get(accountEntityID)
		accountEntity.createCellEntity(self.hallEntity.cell)

	def onAccountGetCell(self, eventType, eventName, senderEntityID, receiverEntityID, args):
		DEBUG_MSG("onAccountGetCell")
